﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dotnet.Utils.Utility.Verify
{
    /// <summary>
    /// 生成验证码工具
    /// </summary>
    /// <remarks>
    /// 此工具用于随机生成验证码，其中验证码为阿拉伯数字与大写英文字母的组合。
    /// 验证码默认长度为4，可根据需要修改。
    /// </remarks>
    public class CheckCodeUtil
    {
        /// <summary>
        /// 定义存储Cookies名称
        /// </summary>
        public const string CHECK_NAME = "CheckCode";

        /// <summary>
        /// 定义验证码的字符个数
        /// </summary>
        public const int DEFAULT_LENGTH = 4;

        /// <summary>
        /// 定义验证码可以使用的字符集合，去除了不易识别的字符
        /// </summary>
        private static readonly char[] LETTERS = {'A','B', 'C', 'D', 'E', 'F', 'G',
                                                     'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'Q', 'R', 'S',
                                                     'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4',
                                                     '5', '6', '7', '8', '9'};

        /// <summary>
        /// 调用此函数返回一个随机生成的验证码，使用默认验证码个数
        /// </summary>
        /// <example>
        /// 生成随机验证码的示例：
        /// <code>
        /// string sCode = CheckCodeUtil.Generate();
        /// </code>
        /// </example>
        /// <returns>验证码（为数字与字母的混合）</returns>
        public static string Generate()
        {
            return Generate(DEFAULT_LENGTH);
        }
        /// <summary>
        /// 调用此函数返回一个随机生成的验证码
        /// </summary>
        /// <example>
        /// 生成随机验证码的示例：
        /// <code>
        /// string sCode = CheckCodeUtil.Generate(6);
        /// </code>
        /// <param name="iCount"><see cref="int"/>，验证码个数</param>
        /// <returns>验证码（为数字与字母的混合）</returns>
        public static string Generate(int iCount)
        {
            //初始化随机生成器
            Random r = new Random();

            //生成验证码
            string ret = string.Empty;
            for (int i = 0; i < iCount; i++)
            {
                ret += LETTERS[r.Next(0, LETTERS.Length)];
            }
            return ret;
        }

        /// <summary>
        /// 生成随机字符(数字+字母)
        /// </summary>
        /// <param name="length">字符长度</param>
        /// <returns>随机字符组成的字符串</returns>
        public static string GetRandomStr(int length)
        {
            char[] arrChar ={
    '0','1','2','3','4','5','6','7','8','9',
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
    };

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            int arrCharLength = arrChar.Length;
            Random rd = new Random();
            for (int i = 0; i < length; i++)
            {
                sb.Append(arrChar[rd.Next(arrCharLength)]);
            }
            return sb.ToString();
        }


        /// <summary>
        /// 生成随机字符
        /// </summary>
        /// <param name="length">字符长度</param>
        /// <param name="number">是否包含数字</param>
        /// <param name="lower">是否包含小写字母</param>
        /// <param name="upper">是否包含大写字母</param>
        /// <param name="symbol">是否包含特殊字母</param>
        /// <returns>随机字符组成的字符串</returns>
        public static string GetRandomstring(int length, bool number = true, bool lower = true, bool upper = true, bool symbol = false)
        {
            char[] numberChars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            char[] lowerChars = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
            char[] upperChars = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            //反斜杠是转义符,\\\" 代表 \"
            char[] symbolChars = "\\\"!#$%&'()*+,-./:;<=>?@[]^_`{|}~".ToArray();

            IEnumerable<char> charList = Enumerable.Empty<char>();
            if (number) charList = charList.Union(numberChars);
            if (lower) charList = charList.Union(lowerChars);
            if (upper) charList = charList.Union(upperChars);
            if (symbol) charList = charList.Union(symbolChars);

            var arrChar = charList.ToArray();
            int arrCharLength = arrChar.Length;
            if (arrCharLength == 0)
            {
                return string.Empty;
            }

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            Random rd = new Random();
            for (int i = 0; i < length; i++)
            {
                sb.Append(arrChar[rd.Next(arrCharLength)]);
            }
            return sb.ToString();
        }


    }
}
